﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using LoungeRepo.Contracts.Core;

namespace LoungeRepo.Core.Extensions
{
    public static class ILoungeRepositoryExtensions
    {
        public static void PrefetchAllEntities(this ILoungeRepository repo)
        {
            var entityFilenames = new RepoEntityFileIterator().Collect(repo.RootFolderPath);
            foreach(string entityFilename in entityFilenames)
            {
                repo.Load(
                    GetEntityIdFromFilename(entityFilename), 
                    GetEntityPartitionFromFilename(entityFilename, repo.RootFolderPath)
                    );
            }
        }


        private static string GetEntityIdFromFilename(string entityFilename)
        {
            return Path.GetFileNameWithoutExtension(entityFilename);
        }

        private static string GetEntityPartitionFromFilename(string entityFilename, string repoPath)
        {
            var partition = Path.GetDirectoryName(entityFilename);
            if (partition == repoPath) return "";
            
            return partition.Replace(repoPath + "\\", "");
        }
    }
}
